<?php

/**
 * 无需登录即可访问的用户相关接口
 */
namespace App\Http\Controllers\Home\API;

use App\Http\Controllers\BaseController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class UserNoNeedTokenController extends BaseController
{
    public function __construct()
    {
        parent::__construct();
        if(!empty($_REQUEST['token'])){
            $token = $_REQUEST['token'];
            $this->loginUserInfo = $this->checkToken($token);
        }
    }

    /**
     * 首页-推荐
     * User: Xiahuiqin
     *
     * @return mixed|string|void
     */
    public function index(Request $request)
    {
        $page = $request->input('page');
        if(empty($page)) $page = 1;
        $start = ($page - 1) * 10;

        $type = $request->input('type');
        $list = [];
        if(empty($type)){
            //首页推荐
            $list = DB::table('member_online')
                ->select('member.id','username','sign','avatar','price','user_role')
                ->leftJoin('member','member.id','=','member_online.member_id')
                ->where('member_online.status',1)
                ->where('member.show_index',1)
                ->where('member.status',1)
                ->where('user_role',3)
                ->where('auth_status',2)
                ->orderBy('member.istop','desc')
                ->orderBy('member_online.status','asc')
                ->skip($start)->take(10)->get();
        }else if($type=='new'){
            //新人
            $list = DB::table('member')
                ->select('id','username','sign','avatar','price','user_role')
                ->where('status',1)
                ->where('user_role',3)
                ->where('auth_status',2)
                ->where('create_time','>',strtotime("-1 month"))
                ->orderBy('auth_deny_time','desc')->skip($start)->take(10)
                ->get();
        }else if($type=='gender'){
            //性别
            $gender = $request->input('gender');
            if(!empty($gender)){
                $list = DB::table('member')
                    ->select('id','username','sign','avatar','price','user_role')
                    ->where('gender',$gender)
                    ->where('status',1)
                    ->where('user_role',3)
                    ->where('auth_status',2)
                    ->orderBy('id','desc')->skip($start)->take(10)
                    ->get();
            }
        }else if($type=='focus'){
            //关注
            if(!empty($this->loginUserInfo->id)){
                $list = DB::table('member_fans')
                    ->select('member.id','member.username','member.sign','member.avatar','member.price','member.user_role')
                    ->leftJoin('member','member.id','=','member_fans.member_id_b')
                    ->where('member.status',1)
                    ->where('member.user_role',3)
                    ->where('member.auth_status',2)
                    ->where('member_fans.member_id_z',$this->loginUserInfo->id)
                    ->orderBy('member_fans.create_time','desc')
                    ->skip($start)->take(10)->get();
            }
        }else if($type=='free'){
            //限免
            $list = DB::table('member')
                    ->select('id','username','sign','avatar','price','user_role')
                    ->where('status',1)
                    ->where('user_role',3)
                    ->where('auth_status',2)
                    ->where('first_free',1)
                    ->orderBy('id','desc')
                    ->skip($start)->take(10)
                    ->get();
        }else if($type=='star'){
            $gj = $request->input('gj');
            if(!empty($gj)){
                //星级
                $findStar = DB::table('admini_systeminfo')->select('cg','jg','yg')->first();
                if(!empty($findStar)){
                    $yg = $findStar->yg;
                    $jg = $findStar->jg;
                    $cg = $findStar->cg;
                    $userList = DB::table('replay')
                        ->select(DB::raw('count(*) as like_num'),'anchor_id')
                        ->where('feel',1)
                        ->groupBy('anchor_id')
                        ->skip($start)->take(10)
                        ->get();
                    $uList = [];
                    if($gj=='yg'){
                        //银冠用户
                        foreach($userList as $k => $v){
                            if($v->like_num>=$yg && $v->like_num<$jg){
                                $uList[] = $v;
                            }
                        }
                    }else if($gj=='jg'){
                        //金冠用户
                        foreach($userList as $k => $v){
                            if($v->like_num>=$jg && $v->like_num<$cg){
                                $uList[] = $v;
                            }
                        }
                    }else if($gj=='cg'){
                        //彩冠用户
                        foreach($userList as $k => $v){
                            if($v->like_num>=$cg){
                                $uList[] = $v;
                            }
                        }
                    }
                    if(!empty($uList)){
                        foreach ($uList as $uk => $uv){
                            $find[$uk] = DB::table('member')
                                ->select('id','username','sign','avatar','price','user_role')
                                ->where('status',1)
                                ->where('id',$uv->anchor_id)
                                ->where('user_role',3)
                                ->where('auth_status',2)->first();
                            if(!empty($find[$uk])){
                                $list[] = $find[$uk];
                            }
                        }
                    }
                }
            }
        }else{
            //首页推荐
            $list = DB::table('member')
                ->select('member.id','username','sign','avatar','price','user_role')
                ->leftJoin('member_online','member.id','=','member_online.member_id')
                ->where('member_online.status',1)
                ->where('member.show_index',1)->where('member.status',1)->where('user_role',3)->where('auth_status',2)->orderBy('member.id','desc')
                ->skip($start)->take(10)->get();
        }

        if(!empty($list)){
            foreach ($list as $lk => $lv){
                $list[$lk]->anchor_status = $this->getOneAnchorStatus($lv->id);
                    if($type != 'star'){
                        $findStar = DB::table('admini_systeminfo')->select('cg','jg','yg')->first();
                        $yg = $findStar->yg;
                        $jg = $findStar->jg;
                        $cg = $findStar->cg;
                        $likeNum = DB::table('replay')->where('feel',1)
                            ->where('anchor_id',$lv->id)
                            ->count();
                        if($likeNum>=$yg && $likeNum<$jg){
                            $list[$lk]->gj = 'yg';
                        }else if($likeNum>=$jg && $likeNum<$cg){
                            $list[$lk]->gj = 'jg';
                        }else if($likeNum >= $cg){
                            $list[$lk]->gj = 'cg';
                        }else{
                            $list[$lk]->gj = 'yg';
                        }
                }
            }
        }
        echo json_encode(['code'=>1,'msg'=>'SUCCESS','data'=>$list]);
    }



    /**
     * 微信QQ第三方登录-用户第三方登录成功后调用的接口
     * User: Xiahuiqin
     *
     * @param Request $request
     *
     * @return mixed|string|void
     */
    public function openidAuth(Request $request)
    {
        $openid = $request->input('openid');
        $auth_type = $request->input('auth_type');
        if(empty($openid) || empty($auth_type) || !in_array($auth_type,[1,2])) return json_encode(['code'=>-1002,'msg'=>'参数不合法']);

        if($auth_type==1){
            //wx
            $findUser = DB::table('member')->where('openid',$openid)->where('auth_type',$auth_type)->where('status',1)->first();
            $insertArr['openid'] = $openid;
        }else{
            //qq
            $findUser = DB::table('member')->where('qqid',$openid)->where('auth_type',$auth_type)->where('status',1)->first();
            $insertArr['qqid'] = $openid;
        }
        $insertArr['auth_type'] = $auth_type;
        $insertArr['status'] = 1;

        if(empty($findUser)){
            //insert
            $userId = DB::table('member')->insertGetId($insertArr);
            $mobile = '';
        }else{
            $userId = $findUser->id;
            $mobile = $findUser->mobile;
        }

        $upArr['token'] = $this->setToken();
        $upArr['time_out'] = time()+3600*24*90;

        //更新用户token和过期时间
        DB::table('member')->where('id',$userId)->update($upArr);

        //登录成功返回token
        $returnData = array(
            'token' => $upArr['token'],
            'mobile' => $mobile,
            'member_id' => $userId
        );
        echo json_encode(['code'=>1,'msg'=>'SUCCESS','data'=>$returnData]);
    }

    /**
     * 用户登录
     * User: Xiahuiqin
     *
     * @param Request $request
     *
     * @return mixed|string|void
     */
    public function login(Request $request)
    {
        $mobile = $request->input('mobile');
        $password = $request->input('password');
//        if(strlen($password) != 32 || !\Helpers::isMobile($mobile)) return json_encode(['code'=>-1002,'msg'=>'参数不合法']);
        if(strlen($password) != 32) return json_encode(['code'=>-1002,'msg'=>'参数不合法']);
        //传过来的加密密码再md5加密一次
        $password = md5($password);

        $findUser = DB::table('member')->where('mobile',$mobile)->where('password',$password)->where('status',1)->first();
        if(empty($findUser)) return json_encode(['code'=>-1005,'msg'=>'帐号密码不匹配']);

        $upArr['token'] = $this->setToken();
        $upArr['time_out'] = time()+3600*24*90;

        //更新用户token和过期时间
        DB::table('member')->where('id',$findUser->id)->update($upArr);

        //登录成功返回token
        $returnData = array(
            'token' => $upArr['token'],
            'mobile' => $findUser->mobile,
            'member_id' => $findUser->id
        );
        echo json_encode(['code'=>1,'msg'=>'SUCCESS','data'=>$returnData]);
    }

    /**
     * 用户注册
     * User: Xiahuiqin
     *
     * @param Request $request
     *
     * @return mixed|string|void
     */
    public function register(Request $request)
    {
        $mobile = $request->input('mobile');
        $password = $request->input('password');
        $code = $request->input('code');  //验证码
        $pid = $request->input('pid');  //用户上游id  - 推荐人id

        //if(strlen($password) != 32 || !\Helpers::isMobile($mobile)) return json_encode(['code'=>-1002,'msg'=>'参数不合法']);
        if(strlen($password) != 32) return json_encode(['code'=>-1002,'msg'=>'参数不合法']);
        if($code != '1234') return json_encode(['code'=>-1003,'msg'=>'验证码有误']);
        //验证短信验证码
//        $checkCode = $this->checkSms($mobile,$code);
//        if($checkCode == 0) return json_encode(['code'=>-1003,'msg'=>'验证码有误']);

        //传过来的加密密码再md5加密一次
        $password = md5($password);

        $findUser = DB::table('member')->where('mobile',$mobile)->where('password',$password)->where('status','!=',3)->first();
        if(!empty($findUser)) return json_encode(['code'=>-1004,'msg'=>'手机号已存在']);

        $insertArr['mobile'] = $mobile;
        $insertArr['password'] = $password;
        $insertArr['user_role'] = 1;
        $insertArr['create_time'] = time();
        if(!empty($pid)) {
            $insertArr['pid'] = $pid;
        }
        $insertId = DB::table('member')->insertGetId($insertArr);
        if($insertId){
            \Cache::forget('sms_'.$mobile);  //让验证码失效
            echo json_encode(['code'=>1,'msg'=>'SUCCESS']);
        }else{
            echo json_encode(['code'=>-1001,'msg'=>'服务器异常']);
        }
    }

    /**
     * 生成token
     * User: Xiahuiqin
     * @return string
     */
    public function setToken()
    {
        $str = md5(uniqid(md5(microtime(true)),true)); //生成一个不会重复的字符串
        $str = sha1($str); //加密
        return $str;
    }

    public function addWeibo(Request $request)
    {
        $post = $request->all();
        $member_id = !empty($post['member_id']) ? $post['member_id'] : '';
        $weibo = !empty($post['weibo']) ? $post['weibo'] : '';
        if(empty($member_id)) return json_encode(['code'=>-1,'msg'=>'参数不合法']);
        if(empty($weibo)) return json_encode(['code'=>-1,'msg'=>'参数不合法']);
        $res = DB::table('member')->where('id',$member_id)->update(['weibo'=>$weibo]);
        if($res || $res === 0)
        {
            return json_encode(['code'=>1,'msg'=>'设置成功']);
        }else{
            return json_encode(['code'=>-1,'msg'=>'设置失败']);
        }
    }

    public function addDouyin(Request $request)
    {
        $post = $request->all();
        $member_id = !empty($post['member_id']) ? $post['member_id'] : '';
        $douyin = !empty($post['douyin']) ? $post['douyin'] : '';
        if(empty($member_id)) return json_encode(['code'=>-1,'msg'=>'参数不合法']);
        if(empty($douyin)) return json_encode(['code'=>-1,'msg'=>'参数不合法']);
        $res = DB::table('member')->where('id',$member_id)->update(['douyin'=>$douyin]);
        if($res || $res === 0)
        {
            return json_encode(['code'=>1,'msg'=>'设置成功']);
        }else{
            return json_encode(['code'=>-1,'msg'=>'设置失败']);
        }
    }

    /**
     * 获取某个主播信息
     * User: Xiahuiqin
     *
     * @param Request $request
     *
     * @return mixed|string|void
     */
    public function getOneAnchor(Request $request)
    {
        $member_id = $request->input('member_id');
        if(empty($member_id)) return json_encode(['code'=>-1002,'msg'=>'参数不合法']);
        $findUser = DB::table('member')->where('id',$member_id)->where('status',1)->first();
        if(empty($findUser)) return json_encode(['code'=>-1009,'msg'=>'数据不存在']);

        $result['username'] = $findUser->username;
        $result['price'] = $findUser->price;
        $result['sign'] = $findUser->sign;
        $result['level'] = $this->getUserLevel($member_id);
        $result['g_level'] = 'yg';
        $result['city'] = $findUser->city;
        $result['height'] = $findUser->height;
        $result['weight'] = $findUser->weight;
        $result['mobile'] = $findUser->mobile;
        $result['avatar'] = empty($findUser->avatar) ? '' : $findUser->avatar;
        $result['first_free'] = $findUser->first_free;
        $result['weibo'] = $findUser->weibo;
        $result['douyin'] = $findUser->douyin;
        $result['constellation'] = $this->get_constellation($findUser->birth);
        //TODO ==== 接听率
        $result['answer_rate'] = "84%";
        $result['last_status'] = $this->getOneAnchorStatus($member_id);
        $result['apperance'] = \Helpers::getApperance($findUser->apperance);  //仙女自评

        $result['like'] = DB::table('replay')->where('anchor_id',$member_id)->where('feel',1)->count();  //喜爱
        $result['unlike'] = DB::table('replay')->where('anchor_id',$member_id)->where('feel',2)->count();  //无感

        $result['comment'] = [];

        $replyList = DB::table('replay')
            ->select('uid','avatar as user_avatar','username','replay_ids')
            ->leftJoin('member','member.id','=','replay.uid')
            ->where('replay.anchor_id',$member_id)
            ->orderBy('replay.create_time','desc')
            ->get();

        if(!empty($replyList)){
            foreach ($replyList as $rk => $rv){
                $replayTag = \Helpers::getReplyTag($rv->replay_ids);
                $replyList[$rk]->reply_tag = !empty($replayTag) ? $replayTag : null;
                $replyList[$rk]->level = $this->getUserLevel($rv->uid);
            }
            $result['comment'] = $replyList;
        }

        //粉丝数
        $result['fans'] =  \Helpers::getUserFans($member_id);
        $result['auth_img'] = DB::table('member_auth_img')->select('img')->where('member_id',$member_id)->where('status',1)->orderBy('id','desc')->get();
        $authorImgArr = DB::table('member_img')->select('img')->where('member_id',$member_id)->where('status',1)->orderBy('id','desc')->get();
        $authorImg = [];
        if(!empty($authorImgArr))
        {
            foreach ($authorImgArr as $airK => $airV)
            {
                $authorImg[] = $airV->img;
            }
        }
        $result['author_img'] = $authorImg;

        if(!empty($this->loginUserInfo->id)){
            //用户是登录状态，判断关注状态
            $is_focus = DB::table('member_fans')->where('member_id_b',$member_id)->where('member_id_z',$this->loginUserInfo->id)->first();
            $result['focus_status'] = 0;
            $result['is_black'] = 0;

            if(!empty($is_focus)){
                if($is_focus->type==1){
                    $result['focus_status'] = 1;
                }else if($is_focus->type==3){
                    $result['is_black'] = 1;
                }
            }
        }else{
            $result['focus_status'] = '';
            $result['is_black'] = '';
        }
        $findStar = DB::table('admini_systeminfo')->select('cg','jg','yg')->first();
        $yg = $findStar->yg;
        $jg = $findStar->jg;
        $cg = $findStar->cg;
        $likeNum = DB::table('replay')
            ->select(DB::raw('count(*) as like_num'))
            ->where('feel',1)
            ->where('anchor_id',$member_id)
            ->first();
        if($likeNum->like_num>=$yg && $likeNum->like_num<$jg){
            $result['gj'] = 'yg';
        }else if($likeNum->like_num>=$jg && $likeNum->like_num<$cg){
            $result['gj'] = 'jg';
        }else{
            $result['gj'] = 'cg';
        }
        echo json_encode(['code'=>1,'msg'=>'SUCCESS','data'=>$result]);
    }

    /**
     * 获取用户礼物柜
     * User: Xiahuiqin
     *
     * @param Request $request
     *
     * @return mixed|string|void
     */
    public function getUserPresent(Request $request)
    {
        $member_id = $request->input('member_id');
        if(empty($member_id)) return json_encode(['code'=>-1002,'msg'=>'参数不合法']);

        $list['total'] = DB::table('member_consume')->select('present_id','e_money')->where('member_id_b',$member_id)->where('type',2)->orderBy('id','desc')->count();
        $list['list'] = DB::table('member_consume')
            ->select(DB::raw('count(*) as present_count'),'member_consume.e_money',DB::raw('concat("'.$this->baseUrl.'",img) as present_img'),'name')
            ->leftJoin('present','present.id','=','member_consume.present_id')
//            ->where('member_consume.member_id_b',$this->loginUserInfo->id)
            ->where('member_consume.member_id_b',$member_id)
            ->where('member_consume.type',2)
            ->groupBy('member_consume.present_id')
            ->orderBy('present.order','asc')
            ->get();

        echo json_encode(['code'=>1,'msg'=>'SUCCESS','data'=>$list]);
    }


    /**
     * 根据生日计算星座
     */
    public function get_constellation($birthday)
    {
        $birthday = date("Y-m-d",strtotime($birthday));
        //传入$birthday格式如：2018-05-06
        $month = intval(substr($birthday, 5, 2));
        $day = intval(substr($birthday, 8, 2));
        if ($month < 1 || $month > 12 || $day < 1 || $day > 31)
        {
            return NULL;
        }
        $signs = array(
            array('20' => '水瓶座'),
            array('19' => '双鱼座'),
            array('21' => '白羊座'),
            array('20' => '金牛座'),
            array('21' => '双子座'),
            array('22' => '巨蟹座'),
            array('23' => '狮子座'),
            array('23' => '处女座'),
            array('23' => '天秤座'),
            array('24' => '天蝎座'),
            array('22' => '射手座'),
            array('22' => '摩羯座')
        );
        list($start, $name) = each($signs[$month - 1]);
        if ($day < $start)
        {
            list($start, $name) = each($signs[($month - 2 < 0) ? 11 : $month - 2]);
        }

        return $name;
    }

    /**
     * 某主播亲密榜-用户给主播花了多少钱的列表
     * User: Xiahuiqin
     *
     * @param Request $request
     *
     * @return mixed|string|void
     */
    public function getCloserList(Request $request)
    {
        $member_id = $request->input('member_id');
        if(empty($member_id)) return json_encode(['code'=>-1002,'msg'=>'参数不合法']);
        //今天
        $dayStart = strtotime(date('Y-m-d 00:00:00'));
        $nowTime = time();
        //一周前时间
        $weekTime = strtotime("-7 Day");
        $listDay = DB::table('member_consume')
            ->select(DB::raw('SUM(e_money) as close_num'),'avatar as user_avatar','username','user_role','member.id as member_id','sign')
            ->leftJoin('member','member.id','=','member_consume.member_id')
            ->where('member_consume.member_id_b',$member_id)
            ->where('member_consume.create_time','>',$dayStart)
            ->where('member_consume.create_time','<',$nowTime)
            ->groupBy('member_consume.member_id')
            ->orderBy('close_num','desc')
            ->get();
        if(!empty($listDay)){
            foreach ($listDay as $k => $v){
                $listDay[$k]->gj = 'yg';
                $listDay[$k]->anchor_status = $this->getOneAnchorStatus($v->member_id);
                $listDay[$k]->treasure = \Helpers::getUserLeftMoney($v->member_id);
                $listDay[$k]->used_money = \Helpers::getUserUsedMoney($v->member_id);
                $listDay[$k]->level = $this->getUserLevel($v->member_id);
            }
        }
        $listWeek = DB::table('member_consume')
            ->select(DB::raw('SUM(e_money) as close_num'),'avatar as user_avatar','username','user_role','member.id as member_id','sign')
            ->leftJoin('member','member.id','=','member_consume.member_id')
            ->where('member_consume.member_id_b',$member_id)
            ->where('member_consume.create_time','>',$weekTime)
            ->where('member_consume.create_time','<',$nowTime)
            ->groupBy('member_consume.member_id')
            ->orderBy('close_num','desc')
            ->get();
        if(!empty($listWeek)){
            foreach ($listWeek as $k1 => $v1){
                $listWeek[$k1]->gj = 'yg';
                $listWeek[$k1]->anchor_status = $this->getOneAnchorStatus($v1->member_id);
                $listWeek[$k1]->treasure = \Helpers::getUserLeftMoney($v1->member_id);
                $listWeek[$k1]->used_money = \Helpers::getUserUsedMoney($v1->member_id);
                $listWeek[$k1]->level = $this->getUserLevel($v1->member_id);
            }
        }
        $data['day'] = !empty($listDay) ? $listDay : [];
        $data['week'] = !empty($listWeek) ? $listWeek : [];
        echo json_encode(['code'=>1,'msg'=>'SUCCESS','data'=>$data]);
    }

    /**
     * 金主榜是按主播收入金币排行 （所有收入）-土豪榜
     * User: Xiahuiqin
     *
     * @param Request $request
     *
     * @return mixed|string|void
     */
    public function getTyrantRank(Request $request)
    {
        $list['day_rank'] = DB::table('member_consume')
            ->select(DB::raw('SUM(e_money) as num'),DB::raw('concat("'.$this->baseUrl.'",avatar) as user_avatar'),'username','user_role','member.id as member_id')
            ->leftJoin('member','member.id','=','member_consume.member_id_b')
            ->where('member.status',1)
            ->whereBetween('member_consume.create_time',[strtotime(date("Y-m-d 00:00:00")),strtotime(date("Y-m-d 23:59:59"))])
            ->groupBy('member_consume.member_id_b')
            ->orderBy('num','desc')
            ->get();
        if(!empty($list['day_rank'])){
            foreach ($list['day_rank'] as $dk => $dv){
                $list['day_rank'][$dk]->gj = 'yg';
                $list['day_rank'][$dk]->level = $this->getUserLevel($dv->member_id);
            }
        }

        $timestamp = time();
        $week_start = strtotime(date('Y-m-d', strtotime("this week Monday", $timestamp)));
        $week_end = strtotime(date('Y-m-d', strtotime("this week Sunday", $timestamp))) + 24 * 3600 - 1;
        $list['week_rank'] = DB::table('member_consume')
            ->select(DB::raw('SUM(e_money) as num'),DB::raw('concat("'.$this->baseUrl.'",avatar) as user_avatar'),'username','user_role','member.id as member_id')
            ->leftJoin('member','member.id','=','member_consume.member_id_b')
            ->where('member.status',1)
            ->whereBetween('member_consume.create_time',[$week_start,$week_end])
            ->groupBy('member_consume.member_id_b')
            ->orderBy('num','desc')
            ->get();
        if(!empty($list['week_rank'])){
            foreach ($list['week_rank'] as $wk => $wv){
                $list['week_rank'][$wk]->gj = 'yg';
                $list['week_rank'][$wk]->level = $this->getUserLevel($wv->member_id);
            }
        }

        $month_start = mktime(0, 0, 0, date('m'), 1, date('Y'));
        $month_end = mktime(23, 59, 59, date('m'), date('t'), date('Y'));
        $list['month_rank'] = DB::table('member_consume')
            ->select(DB::raw('SUM(e_money) as num'),DB::raw('concat("'.$this->baseUrl.'",avatar) as user_avatar'),'username','user_role','member.id as member_id')
            ->leftJoin('member','member.id','=','member_consume.member_id_b')
            ->where('member.status',1)
            ->whereBetween('member_consume.create_time',[$month_start,$month_end])
            ->groupBy('member_consume.member_id_b')
            ->orderBy('num','desc')
            ->get();
        if(!empty($list['month_rank'])){
            foreach ($list['month_rank'] as $mk => $mv){
                $list['month_rank'][$mk]->gj = 'yg';
                $list['month_rank'][$mk]->level = $this->getUserLevel($mv->member_id);
            }
        }
        echo json_encode(['code'=>1,'msg'=>'SUCCESS','data'=>$list]);
    }

    /**
     * 魅力榜是按砖石收入排行 （打赏）-仙女榜
     * User: Xiahuiqin
     *
     * @param Request $request
     *
     * @return mixed|string|void
     */
    public function getFairyRank(Request $request)
    {
        $list['day_rank'] = DB::table('member_consume')
            ->select(DB::raw('SUM(e_money) as num'),DB::raw('concat("'.$this->baseUrl.'",avatar) as user_avatar'),'username','user_role','member.id as member_id')
            ->leftJoin('member','member.id','=','member_consume.member_id_b')
            ->where('member.status',1)
            ->where('member_consume.type',2)
            ->whereBetween('member_consume.create_time',[strtotime(date("Y-m-d 00:00:00")),strtotime(date("Y-m-d 23:59:59"))])
            ->groupBy('member_consume.member_id_b')
            ->orderBy('num','desc')
            ->get();
        if(!empty($list['day_rank'])){
            foreach ($list['day_rank'] as $dk => $dv){
                $list['day_rank'][$dk]->gj = 'yg';
                $list['day_rank'][$dk]->level = $this->getUserLevel($dv->member_id);
            }
        }
        $timestamp = time();
        $week_start = strtotime(date('Y-m-d', strtotime("this week Monday", $timestamp)));
        $week_end = strtotime(date('Y-m-d', strtotime("this week Sunday", $timestamp))) + 24 * 3600 - 1;
        $list['week_rank'] = DB::table('member_consume')
            ->select(DB::raw('SUM(e_money) as num'),DB::raw('concat("'.$this->baseUrl.'",avatar) as user_avatar'),'username','user_role','member.id as member_id')
            ->leftJoin('member','member.id','=','member_consume.member_id_b')
            ->where('member.status',1)
            ->where('member_consume.type',2)
            ->whereBetween('member_consume.create_time',[$week_start,$week_end])
            ->groupBy('member_consume.member_id_b')
            ->orderBy('num','desc')
            ->get();
        if(!empty($list['week_rank'])){
            foreach ($list['week_rank'] as $wk => $wv){
                $list['week_rank'][$wk]->gj = 'yg';
                $list['week_rank'][$wk]->level = $this->getUserLevel($wv->member_id);
            }
        }

        $month_start = mktime(0, 0, 0, date('m'), 1, date('Y'));
        $month_end = mktime(23, 59, 59, date('m'), date('t'), date('Y'));
        $list['month_rank'] = DB::table('member_consume')
            ->select(DB::raw('SUM(e_money) as num'),DB::raw('concat("'.$this->baseUrl.'",avatar) as user_avatar'),'username','user_role','member.id as member_id')
            ->leftJoin('member','member.id','=','member_consume.member_id_b')
            ->where('member.status',1)
            ->where('member_consume.type',2)
            ->whereBetween('member_consume.create_time',[$month_start,$month_end])
            ->groupBy('member_consume.member_id_b')
            ->orderBy('num','desc')
            ->get();
        if(!empty($list['month_rank'])){
            foreach ($list['month_rank'] as $mk => $mv){
                $list['month_rank'][$mk]->gj = 'yg';
                $list['month_rank'][$mk]->level = $this->getUserLevel($mv->member_id);
            }
        }
        echo json_encode(['code'=>1,'msg'=>'SUCCESS','data'=>$list]);
    }

    /**
     * 获取我的小视频
     * User: Xiahuiqin
     *
     * @param Request $request
     *
     * @return mixed|string|void
     */
    public function getUserVideo(Request $request)
    {
        $member_id = $request->input('member_id');
        if(empty($member_id)){
            $member_id = $this->loginUserInfo->id;
        }
        if(empty($member_id)) return json_encode(['code'=>-1002,'msg'=>'参数不合法']);

        $data['free_video'] = DB::table('member_video')->select('id','video','create_time','type','img')->where('member_id',$member_id)->where('type',1)->where('status',1)->orderBy('create_time','desc')->get();
        $data['pay_video'] = DB::table('member_video')->select('id','video','create_time','type','img')->where('member_id',$member_id)->where('type',2)->where('status',1)->orderBy('create_time','desc')->get();
        $data['video_price'] = $GLOBALS['systemInfo']->video_price;
        foreach ($data['free_video'] as $k => $v){
            if($v->type==1){
                //免费视频
                $data['free_video'][$k]->can_see = 1;
            }else{
                //付费视频
                if(empty($this->loginUserInfo->id)){
                    $data['free_video'][$k]->can_see = 0;
                }else{
                    $findCon = DB::table('member_consume')->where('type',3)->where('video_id',$v->id)->where('member_id',$this->loginUserInfo->id)->first();
                    if(!empty($findCon)){
                        $data['free_video'][$k]->can_see = 1;
                    }else{
                        $data['free_video'][$k]->can_see = 0;
                    }
                }
            }
            $data['free_video'][$k]->colletion_num = DB::table('member_video_colletion')->where('video_id',$v->id)->count();
        }

        foreach ($data['pay_video'] as $k1 => $v1){
            if($v1->type==1){
                //免费视频
                $data['pay_video'][$k1]->can_see = 1;
            }else{
                //付费视频
                if(empty($this->loginUserInfo->id)){
                    $data['pay_video'][$k1]->can_see = 0;
                }else{
                    $findCon = DB::table('member_consume')->where('type',3)->where('video_id',$v1->id)->where('member_id',$this->loginUserInfo->id)->first();
                    if(!empty($findCon)){
                        $data['pay_video'][$k1]->can_see = 1;
                    }else{
                        $data['pay_video'][$k1]->can_see = 0;
                    }
                }
            }
            $data['pay_video'][$k1]->colletion_num = DB::table('member_video_colletion')->where('video_id',$v1->id)->count();
        }
        echo json_encode(['code'=>1,'msg'=>'SUCCESS','data'=>$data]);
    }

    /**
     * 获取APP传递过来的主播在线状态
     * User: Xiahuiqin
     *
     * @param Request $request
     *
     * @return mixed|string|void
     */
    public function sendAnchorStatus(Request $request)
    {
        if(!key_exists('status',$_POST) || empty($_POST['member_id']))
        {
            echo json_encode(['code'=>-1,'msg'=>'参数有误']);die;
        }
//        if($_POST['status'] == 1)
//        {
//
//            //获取用户信息
//            $userInfo = DB::table('member')->where('id',$_POST['member_id'])->select('avatar','username')->first();
//            $sendInfo['type'] = 1;
//            $sendInfo['avatar'] = !empty($userInfo->avatar) ? $userInfo->avatar : '';
//            $sendInfo['username'] = !empty($userInfo->username) ? $userInfo->username : '';
//            $sendInfo['anchorname'] = '';
//            $sendInfo['giftimage'] = '';
//            $sendInfo['money'] = '';
//            $help = new \Helpers();
//            $help->sendJpush(json_encode($sendInfo));
//        }
        $anchor_statusAll = DB::table('member_online')->where('member_id',$_POST['member_id'])->first();
        if(!empty($anchor_statusAll))
        {
           $res = DB::table('member_online')->where('member_id',$_POST['member_id'])->update(
                    [
                        'status'=>$_POST['status'],
                        'set_time' => time()
                    ]
           );
            if($res)
            {
                echo json_encode(['code'=>1,'msg'=>'SUCCESS']);
            }else{
                echo json_encode(['code'=>-1,'msg'=>'ERROR']);
            }
        }else{
            $anchor_status[] = $_POST;
            $anchor_status['set_time'] = time();
            $res = DB::table('member_online')->insert($anchor_status);
            if($res)
            {
                echo json_encode(['code'=>1,'msg'=>'SUCCESS']);
            }else{
                echo json_encode(['code'=>-1,'msg'=>'ERROR']);
            }
        }
    }

    /**
     * 获取最新动态接口
     */

    public function getNewOnlineUser()
    {
        //获取1小时内注册用户
        $findStar = DB::table('admini_systeminfo')->select('cg','jg','yg')->first();
        $yg = $findStar->yg;
        $jg = $findStar->jg;
        $cg = $findStar->cg;
        $Time = time()-3600;
//        $Time = 1540592778;
        $regInfo = DB::table('member')
                    ->select('member.create_time','member.id','member.username','member.sign','member.avatar','member.price','member.user_role',DB::RAW('wj_member_online.status as anchor_status'),'mobile')
                    ->leftJoin('member_online','member.id','=','member_online.member_id')
                    ->where('member.user_role','<','3')
                    ->where('member.create_time','>',$Time)
                    ->get();
        if(!empty($regInfo))
        {
            foreach ($regInfo as $k => $v)
            {
                $regInfo[$k]->type = 1;
                        $likeNum = DB::table('replay')
                    ->select(DB::raw('count(*) as like_num'))
                    ->where('feel',1)
                    ->where('anchor_id',$v->id)
                    ->first();
                if($likeNum->like_num>=$yg && $likeNum->like_num<$jg){
                    $regInfo[$k]->gj = 'yg';
                }else if($likeNum->like_num>=$jg && $likeNum->like_num<$cg){
                    $regInfo[$k]->gj = 'jg';
                }else{
                    $regInfo[$k]->gj = 'cg';
                }
            }
        }

        //获取1小时内设置在线用户
        $userOnline = DB::table('member')
                        ->select('member_online.set_time','member.create_time','member.id','member.username','member.sign','member.avatar','member.price','member.user_role',DB::RAW('wj_member_online.status as anchor_status'),'mobile')
                        ->leftJoin('member_online','member_online.member_id','=','member.id')
                        ->where('member.user_role','<',3)
                        ->where('member_online.status',1)
                        ->where('member_online.set_time','>',$Time)
                        ->get();

        if(!empty($userOnline))
        {
            $regInfoNum = (count($regInfo)-1);
            foreach ($userOnline as $k1 => $v1)
            {
                $num = $regInfoNum+($k1+1);
                $regInfo[$num] = $v1;
                $regInfo[$num]->create_time = $v1->set_time;
                unset($regInfo[$num]->set_time);
                $regInfo[$num]->type = 2;
                $likeNum1 = DB::table('replay')
                    ->select(DB::raw('count(*) as like_num'))
                    ->where('feel',1)
                    ->where('anchor_id',$v1->id)
                    ->first();
                if($likeNum1->like_num>=$yg && $likeNum1->like_num<$jg){
                    $regInfo[$num]->gj = 'yg';
                }else if($likeNum1->like_num>=$jg && $likeNum1->like_num<$cg){
                    $regInfo[$num]->gj = 'jg';
                }else{
                    $regInfo[$num]->gj = 'cg';
                }
            }
        }
        //获取1小时内送礼物用户数据
        if(!empty($this->loginUserInfo->id))
        {
            //主播id
            $member_id_b = $this->loginUserInfo->id;
            $getGiftUserInfo = DB::table('member_consume')
                                ->select('member_consume.create_time','member_consume.member_id','member.username','member.avatar','present.name','member.mobile')
                                ->leftJoin('member','member_consume.member_id','=','member.id')
                                ->leftJoin('present','member_consume.present_id','=','present.id')
                                ->where('member_consume.member_id_b','=',$member_id_b)
                                ->where('member_consume.type','=',2)
                                ->where('member_consume.create_time','>',$Time)
                                ->get();
            $regInfoNum2 = (count($regInfo)-1);
            foreach ($getGiftUserInfo as $k2 => $v2)
            {
                $num1 = $regInfoNum2+($k2+1);
                $regInfo[$num1] = $v2;
                $regInfo[$num1]->type = 3;
            }

            //获取我的通话记录
            $getCallInfo = DB::table('member_consume')
                ->select('member_consume.create_time','member_consume.member_id','member.username','member.avatar','present.name','member_consume.chat_time','member_consume.call_id')
                ->leftJoin('member','member_consume.member_id','=','member.id')
                ->leftJoin('present','member_consume.present_id','=','present.id')
                ->where('member_consume.member_id_b','=',$member_id_b)
                ->where('member_consume.type','=',1)
                ->where('member_consume.create_time','>',$Time)
                ->get();
            $regInfoNum3 = (count($regInfo)-1);
            foreach ($getCallInfo as $k3 => $v3)
            {
                $num2 = $regInfoNum3+($k3+1);
                $regInfo[$num2] = $v3;
                $regInfo[$num2]->type = 4;
                $likeNum2 = DB::table('replay')
                    ->select(DB::raw('count(*) as like_num'))
                    ->where('feel',1)
                    ->where('anchor_id',$member_id_b)
                    ->first();
                if($likeNum2->like_num>=$yg && $likeNum2->like_num<$jg){
                    $regInfo[$num2]->gj = 'yg';
                }else if($likeNum2->like_num>=$jg && $likeNum2->like_num<$cg){
                    $regInfo[$num2]->gj = 'jg';
                }else{
                    $regInfo[$num2]->gj = 'cg';
                }
            }
        }
        //1小时内通话记录

        if(!empty($regInfo))
        {
            $regInfo = \Helpers::object_to_array($regInfo);
            $regInfo = $this->my_sort($regInfo,'create_time',SORT_DESC);
            echo json_encode(['code'=>1,'data'=>$regInfo]);
        }else{
            echo json_encode(['code'=>1,'data'=>$regInfo]);
        }

    }

    private function my_sort($arrays,$sort_key,$sort_order=SORT_ASC,$sort_type=SORT_NUMERIC ){
        if(is_array($arrays)){
            foreach ($arrays as $array){
                if(is_array($array)){
                    $key_arrays[] = $array[$sort_key];
                }else{
                    return false;
                }
            }
        }else{
            return false;
        }
        array_multisort($key_arrays,$sort_order,$sort_type,$arrays);
        return $arrays;
    }

    /**
     * 获取主播在线状态（如果有指定主播，则只返回他的状态，否则，返回所有主播状态）
     * User: Xiahuiqin
     *
     * @param Request $request
     *
     * @return mixed|string|void
     */
    public function getAnchorStatus(Request $request)
    {

        $member_id = $request->input('member_id');
        if(!empty($member_id))
        {
            $anchor_statusAll = DB::table('member_online')->where('member_id',$member_id)->first();
            if(!empty($anchor_statusAll))
            {
                echo json_encode(['code'=>1,'msg'=>'SUCCESS','data'=>$anchor_statusAll]);
            }else{
                echo json_encode(['code'=>1,'msg'=>'SUCCESS','data'=>['member_id'=>$member_id,'status'=>0]]);
            }
        }else{
            echo json_encode(['code'=>-1,'msg'=>'ERROR:参数有误']);
        }
    }


    /**
     * 获取形象标签列表
     * User: Xiahuiqin
     *
     * @param Request $request
     *
     * @return mixed|string|void
     */
    public function getAuthSign(Request $request)
    {
        $type = $request->input('type');
        $sign = DB::table('member_auth_sign')->select('id','sign','color')->where('status',1)->orderBy('create_time','desc')->get();
        if(!empty($type)){
            $sign = array_slice($sign,0,6);
        }
        echo json_encode(['code'=>1,'msg'=>'SUCCESS','data'=>$sign]);
    }

    /**
     * tab中的视频
     * User: Xiahuiqin
     *
     * @return mixed|string|void
     */
    public function tabVideo(Request $request)
    {
        $page = $request->input('page');
        if(empty($page)) $page = 1;
        $pageSize = 50;
        $start = ($page - 1) * $pageSize;

        $type = $request->input('type');
        if(empty($type)){
            $memberId = $request->input('member_id');
            //推荐
            $list = DB::table('member_video')->select('id','video','img','type','member_id','view_num')
                    ->where('status',1);
            if(!empty($memberId))
            {
                $list =  $list->where('member_id',$memberId);
            }
            $list =  $list->orderBy('view_num','desc')
                    ->skip($start)->take($pageSize)->get();
        }else if($type=='city'){
            //同城
//            $city = $request->input('city');
//            if(empty($city)){
//                $list = [];
//            }else{
//                $findSameCityUser = DB::table('member')->select('id')->where('city','like','%'.$city.'%')->where('status',1)->orderBy('id','desc')->get();
//                if(empty($findSameCityUser)) {
//                    $list = [];
//                }else{
//                    $cityIds = array_column(\Helpers::object_to_array($findSameCityUser),'id');
//                    $list = DB::table('member_video')->select('id','video','img','type','member_id','view_num')->where('status',1)->whereIn('member_id',$cityIds)->orderBy('id','desc')->skip($start)->take($pageSize)->get();
//                }
//            }
            $list = DB::table('member_video')->select('id','video','img','type','member_id','view_num')->where('status',1)->orderBy('create_time','desc')->skip($start)->take($pageSize)->get();
        }else if($type=='focus'){
            //关注
            if(empty($this->loginUserInfo->id)){
                $list = [];
            }else{
                $list = DB::table('member_video')
                    ->select('member_video.id','member_video.video','member_video.img','member_video.type','member_video.member_id','member_video.view_num')
                    ->leftJoin('member_fans','member_fans.member_id_b','=','member_video.member_id')
                    ->where('member_video.status',1)
                    ->where('member_fans.type',1)
                    ->where('member_fans.member_id_z',$this->loginUserInfo->id)
                    ->orderBy('member_fans.create_time','desc')
                    ->skip($start)->take($pageSize)->get();
            }
        }else{
            //推荐
            $list = DB::table('member_video')->select('id','video','img','type','member_id','view_num')->where('status',1)->orderBy('view_num','desc')->skip($start)->take($pageSize)->get();
        }

        if(empty($list)) $list = [];
        if(!empty($list)){
            foreach ($list as $k => $v){
                if($v->type==1){
                    //免费视频
                    $list[$k]->can_see = 1;
                }else{
                    //付费视频
                    if(empty($this->loginUserInfo->id)){
                        $list[$k]->can_see = 0;
                    }else{
                        $findCon = DB::table('member_consume')->where('type',3)->where('video_id',$v->id)->where('member_id',$this->loginUserInfo->id)->first();
                        if(!empty($findCon)){
                            $list[$k]->can_see = 1;
                        }else{
                            $list[$k]->can_see = 0;
                        }
                    }
                }
                $list[$k]->anchor_status = $this->getOneAnchorStatus($v->member_id);
                $user[$k] = DB::table('member')->select('price','username','sign','avatar','first_free','mobile')->where('id',$v->member_id)->first();
                $list[$k]->price = $user[$k]->price;
                $list[$k]->sign = $user[$k]->sign;
                $list[$k]->username = $user[$k]->username;
                $list[$k]->avatar = $user[$k]->avatar;
                $list[$k]->video_price = $GLOBALS['systemInfo']->video_price;
                $list[$k]->first_free = $user[$k]->first_free;
                $list[$k]->mobile = $user[$k]->mobile;
                $list[$k]->colletion_num = DB::table('member_video_colletion')->where('video_id',$v->id)->count();
                $list[$k]->gift_num = DB::table('member_consume')->where('type',2)->where('member_id_b',$v->member_id)->count();
                $list[$k]->close_list = DB::table('member_consume')
                    ->select(DB::raw('SUM(e_money) as close_num'),DB::raw('concat("'.$this->baseUrl.'",avatar) as user_avatar'),'username','member.id as close_member_id')
                    ->leftJoin('member','member.id','=','member_consume.member_id')
                    ->where('member_consume.member_id_b',$v->member_id)
                    ->groupBy('member_consume.member_id')
                    ->orderBy('close_num','desc')
                    ->take(3)->get();
                $list[$k]->is_focus = 0;
                $list[$k]->is_collection = 0;
                if(!empty($this->loginUserInfo->id)){
                    //当前用户是登录用户
                    //是否收藏过该视频
                    $findVC[$k] = DB::table('member_video_colletion')->where('member_id',$this->loginUserInfo->id)->where('video_id',$v->id)->first();
                    if(!empty($findVC[$k])){
                        $list[$k]->is_collection = 1;
                    }
                    //是否关注过这个主播
                    $findF[$k] = DB::table('member_fans')->where('member_id_z',$this->loginUserInfo->id)->where('member_id_b',$v->member_id)->where('type',1)->first();
                    if(!empty($findF[$k])){
                        $list[$k]->is_focus = 1;
                    }
                }
            }
        }

        echo json_encode(['code'=>1,'msg'=>'SUCCESS','data'=>$list]);
    }

    /**
     * 视频浏览量+1
     * User: Xiahuiqin
     *
     * @param Request $request
     *
     * @return mixed|string|void
     */
    public function addVideoViewNum(Request $request)
    {
        $video_id = $request->input('video_id');
        if(empty($video_id)) return json_encode(['code'=>-1002,'msg'=>'参数不合法']);

        $findOne = DB::table('member_video')->where('id',$video_id)->first();
        if(empty($findOne)) return json_encode(['code'=>-1009,'msg'=>'数据不存在']);

        $res = DB::table('member_video')->where('id',$video_id)->increment('view_num');
        if($res){
            return json_encode(['code'=>1,'msg'=>'SUCCESS']);
        }else{
            return json_encode(['code'=>-1001,'msg'=>'服务器异常']);
        }
    }

    /**
     * 发送短信验证码
     * User: Xiahuiqin
     *
     * @param Request $request
     *
     * @return int|mixed|string|void
     */
    public function sendSms(Request $request)
    {
        $mobile = $request->input('mobile');
        if(empty($mobile)) return json_encode(['code'=>-1002,'msg'=>'参数不合法']);

        $res = $this->sendSmsBase($mobile);
        if($res==-1){
            return json_encode(['code'=>-1001,'msg'=>'短信发送失败']);
        }else{
            return json_encode($res);
        }
    }

    /**
     * 忘记密码1
     * User: Xiahuiqin
     *
     * @param Request $request
     *
     * @return mixed|string|void
     */
    public function forgetPwdOne(Request $request)
    {
        $mobile = $request->input('mobile');
        $code = $request->input('code');  //验证码

        if (!\Helpers::isMobile($mobile)) return json_encode(['code' => -1002, 'msg' => '参数不合法']);

        $findUser = DB::table('member')->where('mobile',$mobile)->where('status',1)->first();

        //提示用户不存在
        if(empty($findUser)) return json_encode(['code'=>-1009,'msg'=>'数据不存在']);

        //验证短信验证码
        $checkCode = $this->checkSms($mobile, $code);
        if ($checkCode == 0) return json_encode(['code' => -1003, 'msg' => '验证码有误']);

        \Cache::forget('sms_'.$mobile);  //让验证码失效
        return json_encode(['code'=>1,'msg'=>'SUCCESS']);
    }

    /**
     * 忘记密码2--设置密码
     * User: Xiahuiqin
     *
     * @param Request $request
     *
     * @return mixed|string|void
     */
    public function resetPwd(Request $request)
    {
        $mobile = $request->input('mobile');
        $password = $request->input('password');
        if(strlen($password) != 32 || !\Helpers::isMobile($mobile)) return json_encode(['code'=>-1002,'msg'=>'参数不合法']);
        //传过来的加密密码再md5加密一次
        $password = md5($password);

        $findUser = DB::table('member')->where('mobile',$mobile)->where('status',1)->first();
        if(!empty($findUser)){
            //修改密码
            $upArr = array(
                'password' => $password
            );
            DB::table('member')->where('id',$findUser->id)->update($upArr);
            return json_encode(['code'=>1,'msg'=>'SUCCESS']);
        }else{
            //提示用户不存在
            return json_encode(['code'=>-1009,'msg'=>'数据不存在']);
        }
    }

    /**
     * 根据手机号拿用户信息
     * User: Xiahuiqin
     *
     * @param Request $request
     *
     * @return mixed|string|void
     */
    public function getUserInfoByMobile(Request $request)
    {
        $mobile = $request->input('mobile');
        if(!\Helpers::isMobile($mobile)) return json_encode(['code'=>-1002,'msg'=>'参数不合法']);

        $findUser = DB::table('member')->where('mobile',$mobile)->where('status',1)->first();
        if(!empty($findUser)){
            $res['member_id'] = $findUser->id;
            $res['avatar'] = $findUser->avatar;
            $res['username'] = $findUser->username;
            $res['level'] = $this->getUserLevel($findUser->id);;
            $res['auth_status'] = $findUser->auth_status;
            $res['price'] = $findUser->price;
            echo json_encode(['code'=>1,'msg'=>'SUCCESS','data'=>$res]);
        }else{
            //提示用户不存在
            return json_encode(['code'=>-1009,'msg'=>'数据不存在']);
        }
    }

    public function getUserInfoCount()
    {
        $data = [];
        //获取日充值总数
        $startDayTime = strtotime(date('Y-m-d 00:00:00'));
        $nowDayRecharge = DB::table('member_recharge')
            ->select(DB::Raw("sum(e_money) as money"))
            ->where('type',1)
            ->where('operate_status',0)
            ->where('create_time','>',$startDayTime)
            ->first();
        $data['nowDayRecharge'] = !empty($nowDayRecharge->money) ? $nowDayRecharge->money : 0 ;
        //获取周充值总数
        $startWeekTime = strtotime(date('Y-m-d', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600))." 00:00:00");
        $nowWeekRecharge = DB::table('member_recharge')
            ->select(DB::Raw("sum(e_money) as money"))
            ->where('type',1)
            ->where('operate_status',0)
            ->where('create_time','>',$startWeekTime)
            ->first();
        $data['nowWeekRecharge'] = !empty($nowWeekRecharge->money) ? $nowWeekRecharge->money : 0 ;
        //年充值总数
        $startYearTime = strtotime(date('Y-01-01 00:00:00'));
        $nowYearRecharge = DB::table('member_recharge')
            ->select(DB::Raw("sum(e_money) as money"))
            ->where('type',1)
            ->where('operate_status',0)
            ->where('create_time','>',$startYearTime)
            ->first();
        $data['nowYearRecharge'] = !empty($nowYearRecharge->money) ? $nowYearRecharge->money : 0 ;
        //日总主播收益总数
        $anchorDayM = DB::table('member_recharge')
            ->select(DB::Raw("sum(e_money) as money"))
            ->leftJoin('member','member.id','=','member_recharge.member_id')
            ->where('member.user_role',3)
            ->where('member.status',1)
            ->where('member_recharge.operate_status',0)
            ->where('member_recharge.create_time','>',$startDayTime)
            ->first();
        $data['anchorDayM'] = !empty($anchorDayM->money) ? $anchorDayM->money : 0 ;
        //周总主播收益总数
        $anchorWeekM = DB::table('member_recharge')
            ->select(DB::Raw("sum(e_money) as money"))
            ->leftJoin('member','member.id','=','member_recharge.member_id')
            ->where('member.user_role',3)
            ->where('member.status',1)
            ->where('member_recharge.operate_status',0)
            ->where('member_recharge.create_time','>',$startWeekTime)
            ->first();
        $data['anchorWeekM'] = !empty($anchorWeekM->money) ? $anchorWeekM->money : 0 ;
        //年总主播收益总数
        $anchorYealM = DB::table('member_recharge')
            ->select(DB::Raw("sum(e_money) as money"))
            ->leftJoin('member','member.id','=','member_recharge.member_id')
            ->where('member.user_role',3)
            ->where('member.status',1)
            ->where('member_recharge.operate_status',0)
            ->where('member_recharge.create_time','>',$startYearTime)
            ->first();
        $data['anchorYealM'] = !empty($anchorYealM->money) ? $anchorYealM->money : 0 ;
        //提现总数
        $txInfo = DB::table('member_recharge')
            ->select(DB::Raw("sum(e_money) as money"))
            ->where('type',5)
            ->where('operate_status',0)
            ->first();
        $data['txInfo'] = !empty($txInfo->money) ? $txInfo->money*-1 : 0 ;
        echo json_encode(['code'=>1,'data'=>$data]);
    }

    public function getUserOneInfoCount(Request $request)
    {
        $post = $request->all();
        if(empty($post['member_id'])) return json_encode(['code'=>-1,'msg'=>'缺少member_id参数']);
        $member_id = !empty($post['member_id']) ? $post['member_id'] : '';
        //获取单主播年月日收益统计
        $anchorInfo = DB::table('member')
            ->select('username','id')
            ->where('id',$member_id)
            ->where('user_role',3)
            ->where('status',1)
            ->first();
        if(!empty($anchorInfo))
        {
            $startDayTime = strtotime(date('Y-m-d 00:00:00'));
            $startWeekTime = strtotime(date('Y-m-d', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600))." 00:00:00");
            $startYearTime = strtotime(date('Y-01-01 00:00:00'));
            //获取日收益总数
            $anchorDayC = DB::table('member_recharge')
                ->select(DB::Raw("sum(e_money) as money"))
                ->leftJoin('member','member.id','=','member_recharge.member_id')
                ->where('member.user_role',3)
                ->where('member.status',1)
                ->where('member_recharge.member_id',$member_id)
                ->where('member_recharge.operate_status',0)
                ->where('member_recharge.create_time','>',$startDayTime)
                ->first();
            $anchorInfo->dayNum = !empty($anchorDayC->money) ? $anchorDayC->money : 0 ;
            //获取周收益总数
            $anchorWeekC = DB::table('member_recharge')
                ->select(DB::Raw("sum(e_money) as money"))
                ->leftJoin('member','member.id','=','member_recharge.member_id')
                ->where('member.user_role',3)
                ->where('member.status',1)
                ->where('member_recharge.member_id',$member_id)
                ->where('member_recharge.operate_status',0)
                ->where('member_recharge.create_time','>',$startWeekTime)
                ->first();
            $anchorInfo->weekNum = !empty($anchorWeekC->money) ? $anchorWeekC->money : 0 ;
            //获取周收益总数
            $anchorYealC = DB::table('member_recharge')
                ->select(DB::Raw("sum(e_money) as money"))
                ->leftJoin('member','member.id','=','member_recharge.member_id')
                ->where('member.user_role',3)
                ->where('member.status',1)
                ->where('member_recharge.member_id',$member_id)
                ->where('member_recharge.operate_status',0)
                ->where('member_recharge.create_time','>',$startYearTime)
                ->first();
            $anchorInfo->yearNum = !empty($anchorYealC->money) ? $anchorYealC->money : 0 ;
        }
        echo json_encode(['code'=>1,'data'=>$anchorInfo]);
    }

    /**
     * 一对一视频消费
     * User: Xiahuiqin
     *
     * @param Request $request
     *
     * @return mixed|string|void
     */
    public function setOneByOneVideoCousume(Request $request)
    {
        $insertArr['member_id'] = $request->input('member_id');
        $insertArr['member_id_b'] = $request->input('anchor_id');
        $insertArr['chat_time'] = $request->input('chat_time');
        $insertArr['e_money'] = $request->input('e_money');

        if(empty($insertArr['member_id']) || empty($insertArr['member_id_b']) || empty($insertArr['chat_time']) || empty($insertArr['e_money'])) return json_encode(['code'=>-1002,'msg'=>'参数不合法']);

        //验证sign合法性
        $isValidSign = $this->checkSign($insertArr['e_money']);
        if(empty($isValidSign)) exit(json_encode(['code'=>-9005,'msg'=>'sign无效']));
        //验证sign合法性

        $member =  DB::table('member')->where('id',$insertArr['member_id'])->where('status',1)->first();
        $anchor =  DB::table('member')->where('id',$insertArr['member_id_b'])->where('status',1)->first();
        if(empty($member) || empty($anchor)) return json_encode(['code'=>-1009,'msg'=>'数据不存在']);
        $leftMoney = \Helpers::getUserLeftMoney($insertArr['member_id']);
        if($leftMoney < $insertArr['e_money']) return json_encode(['code'=>-1,'msg'=>'用户余额不足']);

        $insertArr['type'] = 1;
        $insertArr['create_time'] = time();

        //一对一视频消费，时间限制 ：同一个用户1分钟内只能消费一次
        $timeDiff = $insertArr['create_time']-60;
        $findLatestConsume = DB::table('member_consume')->where('member_id',$insertArr['member_id'])->where('create_time','>',$timeDiff)->where('type',1)->orderBy('id','desc')->first();
        if(!empty($findLatestConsume)){
            return json_encode(['code'=>-1017,'msg'=>'操作过于频繁']);
        }

        $consumeId = DB::table('member_consume')->insertGetId($insertArr);

        $insertRecharge['member_id'] = $insertArr['member_id'];
        $insertRecharge['create_time'] = $insertArr['create_time'];
        $insertRecharge['e_money'] = '-'.$insertArr['e_money'];
        $insertRecharge['operate_status'] = 1;
        $insertRecharge['paid_at'] = $insertArr['create_time'];
        $insertRecharge['type'] = 3;
        $insertRecharge['consume_id'] = $consumeId;
        $insertRecharge['chat_time'] = $insertArr['chat_time'];

        $res = DB::table('member_recharge')->insertGetId($insertRecharge);

        $insertRechargeA['member_id'] = $insertArr['member_id_b'];
        $insertRechargeA['create_time'] = $insertArr['create_time'];
        $insertRechargeA['e_money'] = $insertArr['e_money'];
        $insertRechargeA['operate_status'] = 1;
        $insertRechargeA['paid_at'] = $insertArr['create_time'];
        $insertRechargeA['type'] = 3;
        $insertRechargeA['consume_id'] = $consumeId;
        $insertRechargeA['chat_time'] = $insertArr['chat_time'];
        DB::table('member_recharge')->insertGetId($insertRechargeA);

        //下级消费了，上级分成
        $this->pidShared($insertArr['member_id'],$insertArr['e_money']);

        if($res){
            echo json_encode(['code'=>1,'msg'=>'SUCCESS']);
        }else{
            echo json_encode(['code'=>-1001,'msg'=>'服务器异常']);
        }
    }

    /**
     * 搜索
     * User: Xiahuiqin
     *
     * @return mixed|string|void
     */
    public function searchIndex(Request $request)
    {
        //搜索（名字+个人自评） 比如搜索活泼可爱，那所有标签是活泼可爱的主播都出来 ，名字里含有活泼可爱的主播也出来
        $keywords = $request->input('keywords');
        if(empty($keywords)) return json_encode(['code'=>-1002,'msg'=>'参数不合法']);
        $list1 = DB::table('member')->select('id','username','sign','avatar','price','user_role')->where('status',1)->where('auth_status',2)->where('username','like','%'.$keywords.'%')->orderBy('id','desc')->get();
        if(!empty($list1)){
            $list1 = \Helpers::object_to_array($list1);
        }
        $list2 = [];
        $findId = DB::table('member_auth_sign')->where('sign',$keywords)->where('status',1)->first();
        if(!empty($findId)){
            $signId = $findId->id;
            $list2 = DB::table('member')
                ->select('id','username','sign','avatar','price','user_role')
                ->where('status',1)->where('auth_status',2)
                ->where('apperance','like','%,'.$signId.'%')
                ->orWhere('apperance','like','%'.$signId.',%')
                ->orWhere('apperance',$signId)
                ->orderBy('id','desc')->get();
            if(!empty($list2)){
                $list2 = \Helpers::object_to_array($list2);
            }
        }

        $list = array_merge($list1,$list2);
        if(empty($list)) $list = [];
        if(!empty($list)){
            foreach ($list as $lk => $lv){
                $list[$lk]['anchor_status'] = $this->getOneAnchorStatus($lv['id']);
                $list[$lk]['gj'] = 'yg';
            }
        }
        echo json_encode(['code'=>1,'msg'=>'SUCCESS','data'=>$list]);
    }

    public function version()
    {
        $data = [];
        $data['code'] = 1;
        $data['data']['versioncode'] = 5;
        $data['data']['downloadurl'] = 'https://'.$_SERVER['HTTP_HOST'].'/upload/app/echat.apk';
        echo json_encode($data);
    }

    public function setUnonline()
    {
        $time = time() - 1800;
        DB::table('member_online')->where('set_time','<',$time)->update(['status'=>0]);
    }
}